/*
 * @Description  : 面包屑导航组件
 * @Author       : Keyman
 * @Date         : 2025-11-11 22:34:12
 * @LastEditors  : Keyman
 * @LastEditTime : 2025-11-21 00:33:03
 */

import { Breadcrumb } from "antd";
import { useSelector } from "react-redux";
import { useLocation } from "react-router-dom";

type MenuItem = {
  key: string;
  label: string;
  children?: MenuItem[];
};

function findPath(
  currentPath: string,
  items: MenuItem[],
  result: string[]
): string[] {
  for (const item of items) {
    if (currentPath.startsWith(item.key)) {
      result.push(item.label);
      if (item.children) {
        findPath(currentPath, item.children, result);
      }
      break;
    }
  }
  return result;
}

function findBreadCrumbPath(menuList: MenuItem[], path: string): string[] {
  const pathSegments: string[] = [];

  return findPath(path, menuList, pathSegments);
}

function BreadCrumb() {
  const location = useLocation();
  const { menuList } = useSelector((state: any) => state.authSlice);
  const items = findBreadCrumbPath(menuList, location.pathname).map(
    (pathSegment) => {
      return {
        title: pathSegment,
      };
    }
  );
  return <Breadcrumb items={items} className="mt mb" />;
}

export default BreadCrumb;
